import 'package:get/get.dart';
import 'package:logger_util/logger_util.dart';
import 'package:url_launcher/url_launcher.dart';

import '../../config/constant.dart';
import '../../net/home_connect.dart';
import '../../widget/scroll_page_view.dart';

class CommunityInfoController extends GetxController {
  var banners = <String>[].obs;
  var bannerIndex = 0.obs;

  var publishTime = ''.obs;

  var title = ''.obs;

  var desc = ''.obs;

  String phone = '';

  int serviceType = -1;

  late ScrollPageController bannerController;
  late HomeConnect _connect;

  @override
  void onInit() {
    _loadNetworkData();
    super.onInit();
  }

  void _loadNetworkData() {
    _connect = Get.find<HomeConnect>();

    int id = Get.arguments[ArgumentKey.id] ?? -1;
    int serviceType = Get.arguments[ArgumentKey.serviceType] ?? -1;
    if (id == -1 || serviceType == -1) {
      logE('获取服务id和服务类型失败: id--->$id  serviceType--->$serviceType');
      return;
    }

    this.serviceType = serviceType;
    _connect.getCommunityDetail(id: id, serviceType: serviceType).then((value) {
      publishTime.value = value.publishTime;
      title.value = value.title;
      desc.value = value.describe;
      phone = value.contactWay;

      banners.clear();
      banners.value = value.showImages;
      bannerController = ScrollPageController(initialPage: banners.length);
      if (banners.isNotEmpty) {
        bannerIndex.value = 0;
      }
    });
  }

  void performClickCallPhone() {
    launchUrl(Uri.parse('tel:$phone'));
  }
}
